package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.service.AdminService;
import com.qf.service.BrandService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin("*")
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private AdminService adminService;

    @Autowired
    private BrandService brandService;

    /**
     * page=1&limit=20&sort=add_time&order=desc
     * @return
     */
    @GetMapping("/list")
    public Object queryBrandAll(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer limit,
                                @RequestParam(defaultValue = "daa_time") String sort,
                                @RequestParam(defaultValue = "desc") String order){
        IPage<DtsBrand> iPage = brandService.queryBrandAll(page, limit, sort, order);
        long total = iPage.getTotal();
        List<DtsBrand> records = iPage.getRecords();
        Map<String,Object> data=new HashMap<>();
        data.put("total",total);
        data.put("items",records);
        return ResponseUtil.ok(data);

    }

    /**
     * 品牌和管理员列表
     * @return
     */
    @GetMapping("/catAndAdmin")
    public Object catAndAdmin(){
        List<CatVo> admin = adminService.queryAdmin();

        List<CatVo> catVos = brandService.queryBrand();
        List<DtsCategory> dtsCategories = brandService.queryCategory();

        Map<String,Object> data=new HashMap<>();
        data.put("adminList",admin);
        data.put("categoryList",querycategoryList(catVos,dtsCategories));
        return ResponseUtil.ok(data);
    }
    /**
     *
     * @param catVos
     * @param dtsCategories
     * @return
     */
    public List<CatVo> querycategoryList(List<CatVo> catVos ,List<DtsCategory> dtsCategories){
        if (catVos==null||dtsCategories==null){
            throw new RuntimeException("数据为空");
        }
        for (int i = 0; i < catVos.size(); i++) {
            List  children= new ArrayList();
            for (int j = 0; j < dtsCategories.size(); j++) {
                if (catVos.get(i).getValue().equals(dtsCategories.get(j).getPid())){
                    CatVo catVo = new CatVo();
                    catVo.setValue(dtsCategories.get(j).getId());
                    catVo.setLabel(dtsCategories.get(j).getName());
                    children.add(catVo);
                    catVos.get(i).setChildren(children);
                }
            }
        }
        return catVos;

    }

    /**
     *品牌制造商添加
     * @param dtsBrand
     * @return
     * @throws Exception
     */
    @PostMapping("/create")
    public Object add(@RequestBody DtsBrand dtsBrand)throws Exception{

        DtsBrand dtsBrand1 = brandService.addBrand(dtsBrand);
        return ResponseUtil.ok(dtsBrand1);
    }

    /**
     *  品牌制造商修改
     * @param dtsBrand
     * @return
     */
    @PostMapping("/update")
    public Object updateBrand(@RequestBody DtsBrand dtsBrand){
        int count = brandService.updateBrand(dtsBrand);
        if (count==0){
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok(dtsBrand);
    }

    /**
     * 品牌制造商删除
     * @param dtsBrand
     * @return
     */
    @PostMapping("/delete")
    public Object deleteBrand(@RequestBody DtsBrand dtsBrand){


        brandService.deleteBrand(dtsBrand);
        return ResponseUtil.ok();
    }
}
